package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.enums.CustomerCollectEnum;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.ProductOptType;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.customer.CustomerInfo;
import com.probox.common.entity.mapper.customer.CustomerCollectMapper;
import com.probox.common.entity.mapper.customer.CustomerInfoMapper;
import com.probox.common.entity.service.customer.ICustomerCollectService;
import com.probox.common.entity.service.product.IProductInfoService;
import com.probox.customer.api.domain.CustomerCollect;
import com.probox.order.api.domain.OrderQuery;
import com.probox.product.api.domain.ProductInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 客户收藏记录Service业务层处理
 *
 * @author zsy
 * @date 2021-06-26
 */
@Service
@Transactional
public class CustomerCollectServiceImpl extends ServiceImpl<CustomerCollectMapper, CustomerCollect> implements ICustomerCollectService {
    @Autowired
    private CustomerCollectMapper customerCollectMapper;

    @Autowired
    private CustomerInfoMapper customerInfoMapper;


    @Autowired
    private IProductInfoService iProductInfoService;

    /**
     * 查询客户收藏记录
     *
     * @param collectUid 客户收藏记录ID
     * @return 客户收藏记录
     */
    @Override
    public CustomerCollect selectCustomerCollectById(Long collectUid) {
        return this.getById(collectUid);
    }

    /**
     * 查询客户收藏记录列表
     *
     * @param customerCollect 客户收藏记录
     * @return 客户收藏记录
     */
    @Override
    public List<CustomerCollect> queryList(CustomerCollect customerCollect) {
        QueryWrapper<CustomerCollect> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(customerCollect.getStartDate())){
            queryWrapper.ge("collect_time",customerCollect.getStartDate());
            queryWrapper.le("collect_time",customerCollect.getEndDate());
        }
        if (StringUtils.isNotEmpty(customerCollect.getCustomerName())){
            queryWrapper.like("customer_name",customerCollect.getCustomerName());
        }
        if (StringUtils.isNotEmpty(customerCollect.getProductNo())){
            queryWrapper.like("product_no",customerCollect.getProductNo());
        }
        if (StringUtils.isNotEmpty(customerCollect.getObjName())){
            queryWrapper.like("obj_name",customerCollect.getObjName());
        }
        queryWrapper.orderByDesc("collect_time");
        return customerCollectMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户收藏记录
     *
     * @param customerCollect 客户收藏记录
     * @return 结果
     */
    @Override
    public boolean insertCustomerCollect(CustomerCollect customerCollect) {

        return this.save(customerCollect);

    }

    /**
     * 修改客户收藏记录
     *
     * @param customerCollect 客户收藏记录
     * @return 结果
     */
    @Override
    public boolean updateCustomerCollect(CustomerCollect customerCollect) {

        return this.updateById(customerCollect);
    }

    /**
     * 批量删除客户收藏记录
     *
     * @param collectUids 需要删除的客户收藏记录ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerCollectByIds(Long[] collectUids) {
        return this.removeByIds(Arrays.asList(collectUids));
    }

    /**
     * 查询当前商品是否被收藏
     *
     * @param customerCollect
     * @return
     */
    @Override
    public Boolean getIsCollectGoods(CustomerCollect customerCollect) {
        //商品id
        Long objUid = customerCollect.getObjUid();
        //客户id
        Long customerUid = customerCollect.getCustomerUid();
        QueryWrapper<CustomerCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("OBJ_TYPE", "good");
        queryWrapper.eq("OBJ_UID", objUid);
        queryWrapper.eq("CUSTOMER_UID", customerUid);
        CustomerCollect collectInfo = customerCollectMapper.selectOne(queryWrapper);
        if (collectInfo != null) {
            if ("1".equals(collectInfo.getCollectStatus())) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        }
        return Boolean.FALSE;
    }


    @Override
    public String batchCollect(CustomerCollect customerCollect) {
        Long customerUid = SecurityUtils.getCustomerUid();
        QueryWrapper<CustomerCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("OBJ_TYPE", customerCollect.getObjType());
        queryWrapper.eq("OBJ_UID", customerCollect.getObjUid());
        queryWrapper.eq("CUSTOMER_UID", customerUid);
        CustomerCollect collect = customerCollectMapper.selectOne(queryWrapper);
        if (collect == null) {
            CustomerCollect collectInfo = new CustomerCollect();
            collectInfo.setPlatformUid(SecurityUtils.getCustomerPlatformUid());
            collectInfo.setObjType(customerCollect.getObjType());
            collectInfo.setObjUid(customerCollect.getObjUid());
            ProductInfoVo ProductData = iProductInfoService.getProductDetail(customerCollect.getObjUid());
            collectInfo.setObjName(ProductData.getProductName());
            collectInfo.setObjImgUrl(ProductData.getDetailImg());
            collectInfo.setCollectStatus("1");
            collectInfo.setCollectTime(DateUtil.date());
            collectInfo.setCustomerUid(customerUid);
            CustomerInfo customerInfo = customerInfoMapper.selectById(customerUid);
            collectInfo.setCustomerName(customerInfo.getCustomerName());
            collectInfo.setCustomerHeadUrl(customerInfo.getHeadImage());
            collectInfo.setProductNo(ProductData.getProductNo());
            collectInfo.setNumbers(ProductData.getNumbers());
            collectInfo.setLunwenType(ProductData.getLunwenType());
            if (StringUtils.isNotEmpty(ProductData.getDownloadFile())) {
                int pos = ProductData.getDownloadFile().lastIndexOf(".");
                if (pos>0) {
                    collectInfo.setShowFile(ProductData.getDownloadFile().substring(pos + 1));
                }
            }
            customerCollectMapper.insert(collectInfo);
        }
        return "";
    }

    /**
     * 收藏或取消收藏
     *
     * @param customerCollect
     * @return
     */
    @Override
    public String editCollectStatus(CustomerCollect customerCollect) {
        Long customerUid = SecurityUtils.getCustomerUid();
        QueryWrapper<CustomerCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("OBJ_TYPE", customerCollect.getObjType());
        queryWrapper.eq("OBJ_UID", customerCollect.getObjUid());
        queryWrapper.eq("CUSTOMER_UID", customerUid);
        CustomerCollect collect = customerCollectMapper.selectOne(queryWrapper);
        if (collect != null) {
            if (collect.getCollectStatus().equals(DataStatus.valid.getKey())){
                collect.setCollectStatus(DataStatus.invalid.getKey());
            }else{
                collect.setCollectStatus(DataStatus.valid.getKey());
            }
            collect.setCollectTime(new Date());
            collect.setUpdateTime(DateUtil.date());
            customerCollectMapper.updateById(collect);
        } else {
            CustomerCollect collectInfo = new CustomerCollect();
            collectInfo.setPlatformUid(SecurityUtils.getCustomerPlatformUid());
            collectInfo.setObjType(customerCollect.getObjType());
            collectInfo.setObjUid(customerCollect.getObjUid());
            ProductInfoVo ProductData = iProductInfoService.getProductDetail(customerCollect.getObjUid());
            collectInfo.setObjName(ProductData.getProductName());
            collectInfo.setObjImgUrl(ProductData.getDetailImg());
            collectInfo.setCollectStatus("1");
            collectInfo.setCollectTime(DateUtil.date());
            collectInfo.setCustomerUid(customerUid);
            CustomerInfo customerInfo = customerInfoMapper.selectById(customerUid);
            collectInfo.setCustomerName(customerInfo.getCustomerName());
            collectInfo.setCustomerHeadUrl(customerInfo.getHeadImage());
            collectInfo.setProductNo(ProductData.getProductNo());
            collectInfo.setNumbers(ProductData.getNumbers());
            collectInfo.setLunwenType(ProductData.getLunwenType());
            if (StringUtils.isNotEmpty(ProductData.getDownloadFile())) {
                int pos = ProductData.getDownloadFile().lastIndexOf(".");
                if (pos>0) {
                    collectInfo.setShowFile(ProductData.getDownloadFile().substring(pos + 1));
                }
            }
            customerCollectMapper.insert(collectInfo);
        }

        return customerCollect.getCollectStatus();
    }

    /**
     * 查询当前客户所有商品收藏记录
     *
     * @return
     */
    @Override
    public List<CustomerCollect> getCustomerAllCollect(CustomerCollect customerCollect) {
        QueryWrapper<CustomerCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PLATFORM_UID", SecurityUtils.getCustomerPlatformUid());
        queryWrapper.eq("OBJ_TYPE", CustomerCollectEnum.good.getKey());
        queryWrapper.eq("COLLECT_STATUS", "1");
        queryWrapper.eq("CUSTOMER_UID", SecurityUtils.getCustomerUid());
        if (StringUtils.isNotEmpty(customerCollect.getObjName())){
            queryWrapper.like("OBJ_NAME", customerCollect.getObjName());
        }
        if (StringUtils.isNotEmpty(customerCollect.getStartDate())){
            queryWrapper.ge("COLLECT_TIME",customerCollect.getStartDate());
            queryWrapper.le("COLLECT_TIME",customerCollect.getEndDate());
        }
        List<CustomerCollect> customerCollectList = customerCollectMapper.selectList(queryWrapper);
        return customerCollectList;
    }

    @Override
    public List<CustomerCollect> getCustomerCollect(Long customerUid) {
        QueryWrapper<CustomerCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("OBJ_TYPE", CustomerCollectEnum.good.getKey());
        queryWrapper.eq("COLLECT_STATUS", "1");
        queryWrapper.eq("CUSTOMER_UID", customerUid);
        List<CustomerCollect> customerCollectList = customerCollectMapper.selectList(queryWrapper);
        return customerCollectList;
    }

    /**
     * 查询当前客户所有商品收藏记录总条数
     *
     * @return
     */
    @Override
    public Integer getCustomerAllCollectCount(CustomerCollect customerCollect) {
        QueryWrapper<CustomerCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PLATFORM_UID", SecurityUtils.getCustomerPlatformUid());
        queryWrapper.eq("OBJ_TYPE", CustomerCollectEnum.good.getKey());
        queryWrapper.eq("COLLECT_STATUS", "1");
        queryWrapper.eq("CUSTOMER_UID", SecurityUtils.getCustomerUid());
        Integer customerCollectListCount = customerCollectMapper.selectCount(queryWrapper);
        return customerCollectListCount;
    }

    @Override
    public Long countAmount(OrderQuery query) {
        return customerCollectMapper.countAmount(query);
    }


}
